<?php
class ControllerSaleCustomerGrade extends AdminController {

	public function index() {
		M('sale/customer_grade');
		$this->getList();
	}

	public function insert() {

		M('sale/customer_grade');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_sale_customer_grade->addCustomerGrade($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/customer_grade'));
		}

		$this->getForm();
	}

	public function update() {

		M('sale/customer_grade');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_sale_customer_grade->editCustomerGrade($this->request->get['customer_grade_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/customer_grade'));
		}

		$this->getForm();
	}

	public function delete() {

		M('sale/customer_grade');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $customer_grade_id) {
				$this->model_sale_customer_grade->deleteCustomerGrade($customer_grade_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/customer_grade'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$customer_grade_total = $this->model_sale_customer_grade->getTotalCustomerGrades();

		$this->data['customer_grades'] = $this->model_sale_customer_grade->getCustomerGrades($qf);

		$this->data['pagination'] = $this->pagination($customer_grade_total)->render();

		$this->display('sale/customer_grade_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (!isset($this->request->get['customer_grade_id'])) {
			$this->data['action'] = UA('sale/customer_grade/insert');
		} else {
			$this->data['action'] = UA('sale/customer_grade/update', 'customer_grade_id=' . $this->request->get['customer_grade_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_sale_customer_grade->getCustomerGrade($this->request->get['customer_grade_id']);
				$this->vo->data['customer_grade_description'] = $this->model_sale_customer_grade->getCustomerGradeDescriptions($this->request->get['customer_grade_id']);
			}
		}

		$this->data['languages'] = C('cache_language_2');

		$this->display('sale/customer_grade_form.tpl');
	}

	protected function validateForm() {
		$pass = true;
		foreach ($this->request->post['customer_grade_description'] as $language_id => $value) {
			if (!range_length($value['name'], 3, 32)) {
				$this->setMessage('error_name_' . $language_id, $this->_['error_name']);
				$pass = false;
			}
		}
		return $pass;
	}

	protected function validateDelete() {

		M('sale/customer');

		foreach ($this->request->post['selected'] as $customer_grade_id) {
			$customer_total = $this->model_sale_customer->getTotalCustomersByGradeId($customer_grade_id);

			if ($customer_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_customer'], $customer_total));
				return false;
			}
		}

		return true;
	}
}